var stationSingleKWHDayData = [];
var totalPage = 0;
var totalCnt = 0;
var lastheaderNameEN = undefined;
var equipmentNameList = [];
var totalChargingKWHList = [];
var chargingCountTotalList = [];
var totalChargingDTTMList = [];
var dataList = [];
var myChart = "";

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("stationSingleKWHDayThead", stationSingleKWHDayTable, search);
	scrollTableSuspend("scrollBody", "stationSingleKWHDayTable", "stationSingleKWHDayTable1", "stationSingleKWHDayThead", "scrollHeight");
	selectDateTime("startDate");
	selectDateTime("endDate");
	$("#startChargingTime").val(date2String(getSystemTime()) + " 00:00");
	$("#endChargingTime").val(date2String(getSystemTime()) + " 23:59");
	$ajaxList('chargingStation', getChargingStation, {BulidStatus: '3'});
	search();
});
function getChargingStation(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#chargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
	$("#chargingStationID").attr('size', (length + 1) > 10 ? 10 : (length + 1));
}
function search(headerNameEN){
	var startChargingTime = $("#startChargingTime").val();
	var endChargingTime = $("#endChargingTime").val();
	if($("#chargingStationID").val() == ""){
		setTimeout(hideLoading, 500);
		$("#histogram").empty();
		$("#contenter").attr("hidden", true);
		return;
	}
	$("#chargingStationID").attr('size', 0);
	equipmentNameList = [];
	totalChargingKWHList =[];
	chargingCountTotalList = [];
	totalChargingDTTMList = [];
	if(lastheaderNameEN != headerNameEN){
		clearOrder(lastheaderNameEN);
		lastheaderNameEN = headerNameEN;
	}
	if (stringToDate(startChargingTime) > stringToDate(endChargingTime)){
		showMessge("开始时间不能大于结束时间！");
		$("#contenter").attr("hidden",true);
	} else {
		$ajax('searchStationTotalKWHDay', 'stationSingleKWHDaySearch', searchStationSingleKWHDay);
	}
}
function searchStationSingleKWHDay(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	$("#totalKWH").html(data.totalChargingKWHDay);
	$("#totalCount").html(data.totalChargingCount);
	$("#totalTime").html(formatTime(data.totalChargingTime) || "0");
	stationSingleKWHDayData = data && data.stationTotalKWHDay || [];
	createTableBody("stationSingleKWHDayTbody", stationSingleKWHDayTable, stationSingleKWHDayData);
	for(var i=0; i<stationSingleKWHDayData.length; i++){
		equipmentNameList.push("#" + stationSingleKWHDayData[i].equipmentName.replace(/[^0-9]/ig,""));
		totalChargingKWHList.push(stationSingleKWHDayData[i].chargingKWHTotal);
		chargingCountTotalList.push(stationSingleKWHDayData[i].chargingCountTotal);
		totalChargingDTTMList.push(stationSingleKWHDayData[i].totalChargingDTTM);
	}
	changeEchart();
	setTimeout(hideLoading, 500);
}
function changeEchart(){
	$("#histogram").empty();
	if(equipmentNameList.length == 0){
		$("#contenter").attr("hidden",true);
		return;
	}
	$("#contenter").attr("hidden",false);
	myChart = echarts.init(document.getElementById('histogram'));
	var listName = $("#selectAllList").val();
	var theadName = undefined;
	var color = undefined;
	if(listName == "chargingKWHTotalList"){
		dataList = totalChargingKWHList;
		theadName = "充电电量(KWH)";
		color = '#09be09';
	} else if(listName == "chargingCountTotalList"){
		dataList = chargingCountTotalList;
		theadName = "充电次数(次)";
		color = '#01b2ac';
	} else if(listName == "totalChargingDTTMList"){
		dataList = totalChargingDTTMList;
		theadName = "充电时长(秒)";
		color = '#fe902f';
	}
    var option = {
            tooltip: {
                trigger: 'axis'
            },
            xAxis: [
                {
                	name: '充电桩',
                    type: 'category',
                    data: equipmentNameList
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    name: theadName,
                    min: 0,
                    axisLabel: {
                        formatter: '{value} '
                    }
                }
            ],
            series: [
                {
                    name:theadName,
                    type:'bar',
                    barWidth:10,
                    itemStyle: {
                        normal : {
                        	color: color,
                            label: {
                                show: true,
                                position: 'top',
                                formatter: '{c}'
                            }
                        }
                    },
                    data:dataList
                }
            ]
        };
    myChart.setOption(option);
}